#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: shijianbo
@Email:
@Time: 2022/7/26 16:33
@File: polymerize.py
"""
import os
from warnings import warn


class PolymerzeUtil(object):
    '''
    将所有类聚合到这个类下,用于blade调用
    '''
    DICT_UI = {}
    DICT_FUNC = {}
    FIRST = True

    def __init__(self):
        if self.__class__.FIRST:
            for _system in ('Common',):# 'RCMP', 'SRM',
                for root, dirs, files in os.walk(__file__[:-13] + f"../../{_system}"):
                    if root.split('\\')[-1] == '__pycache__' or '_logs' in root:
                        continue
                    for file_name in files:
                        if file_name in ('testUnit.py',) or file_name.endswith('.jar'):
                            continue
                        path = root.split('/')[-1].replace('\\', '.') + '.' + file_name[:-3]
                        _ = __import__(path, fromlist=['*'])
                        for func in dir(_):
                            if func.startswith('__') and func.endswith('__'):
                                continue
                            if _system in ('RCMP', 'SRM'):
                                if hasattr(eval(f'_.{func}'), 'PATH'):
                                    if func in self.__class__.DICT_UI and id(self.__class__.DICT_UI[func])!=id(eval(f'_.{func}')):
                                        warn(f'[{func}]已经存在于聚合字典,名称重复!!!')
                                    self.__class__.DICT_UI.update({
                                        func: eval(f'_.{func}')
                                    })
                            elif _system in ('Common',):
                                if func in self.__class__.DICT_FUNC and id(self.__class__.DICT_FUNC[func]) != id(eval(f'_.{func}')):
                                    warn(f'[{func}]已经存在于聚合字典,名称重复!!!')
                                self.__class__.DICT_FUNC.update({
                                    func: eval(f'_.{func}')
                                })
            self.__class__.FIRST = False

    def __getitem__(self, item):
        if item in self.__class__.DICT_UI:
            return self.__class__.DICT_UI[item]
        else:
            return self.__class__.DICT_FUNC[item]

if __name__ == '__main__':
    PolymerzeUtil()
